const express = require('express')
const passport = require('passport')
const expressJoi = require('@escook/express-joi')
const db = require('../../db')
const router = express.Router()
const {roleapply_apply_role_schema, audit_id_type_schema} = require('../../schema/role')
router.get('/test', (req, res) => res.json('ok'))
//权限申请
router.post('/roleapply', passport.authenticate('jwt', {session: false}), expressJoi(roleapply_apply_role_schema), (req, res) => {
    const sql = 'select * from roles where user_id = ?'
    db.query(sql, req.user.id, (err, results) => {
        if(err) return res.json({status: 1, message: err.message})
        if(results.length === 0){
            const sqlStr = 'insert into roles set ?'
            const obj = {
                user_id: req.user.id,
                name: req.user.name,
                role: req.user.role,
                apply_role: req.body.apply_role
            }
            db.query(sqlStr, obj, (err, results) => {
                if(err) return res.json({status: 1, message: err.message})
                if(results.affectedRows !== 1) res.json({status: 1, message: '申请失败'})
                res.json({status: 0, message: '申请成功'})
            })
        }else {
            if(results[results.length - 1].apply_status === 0){
                return res.json({status: 1, message: '有未审核的申请'})
            }
            const sqlStr = 'insert into roles set ?'
            const obj = {
                user_id: req.user.id,
                name: req.user.name,
                role: req.user.role,
                apply_role: req.body.apply_role
            }
            db.query(sqlStr, obj, (err, results) => {
                if(err) return res.json({status: 1, message: '执行sql语句失败'})
                if(results.affectedRows !== 1) res.json({status: 1, message: '申请失败'})
                res.json({status: 0, message: '申请成功'})
            })
        }
    })
})
//审核申请(管理员) type: 0 同意 1 驳回
router.post('/audit/:id/:type', passport.authenticate('jwt', {session: false}), expressJoi(audit_id_type_schema), (req, res) => {
    if(req.user.role !== 1) return res.json({status:1, message: '权限不足'})
    const sql = 'select * from roles where id = ?'
    db.query(sql, req.params.id, (err, result) => {
        if(err) return res.json({status: 1, message: '执行sql语句失败'})
        if(result.length !== 1) return res.json({status: 1, message: '该申请不存在'})
        if(result[0].apply_status !== 0) return res.json({status: 1, message: '该申请已审核'})
        if(req.params.type == 1){//驳回
            const sqlStr = 'update roles set apply_status = 2 where id = ?'
            db.query(sqlStr, req.params.id, (err, results) => {
                if(err) return res.json({status: 1, message: err.message})
                if(results.affectedRows !== 1) return res.json({status: 1, message: '驳回失败'})
                res.json({status: 0, message: '驳回成功'})
            })
        }else { //批准
            const sqlStr = 'update roles set role = ?, apply_status = 1 where id = ?'
            db.query(sqlStr, [result[0].apply_role, req.params.id], (err, results) => {
                if(err) return res.json({status: 1, message: '执行sql语句失败'})
                if(results.affectedRows !== 1) return res.json({status: 1, message: '批准失败'})
                const sq = 'update users set role = ? where id = ?'
                db.query(sq, [result[0].apply_role, result[0].user_id], (err, results) => {
                    if(err) return res.json({status: 1, message: err.message})
                    if(results.affectedRows !== 1) return res.json({status: 1, message: '批准失败'})
                    res.json({status: 0, message: '批准成功'})
                })
            })
        }
    })
})
//获取个人申请列表
router.get('/applylist',passport.authenticate('jwt', {session: false}), (req, res) => {
    const sqlStr = 'select * from roles where user_id = ?'
    db.query(sqlStr, req.user.id, (err, results) => {
        if(err) return res.json({status: 1, message: err.message})
        if(results.length === 0) return res.json({status: 1, message: '暂无任何申请'})
        res.json(results)
    })
})
//获取所有员工申请列表(管理员)
router.get('/roleapplylist',passport.authenticate('jwt', {session: false}), (req, res) => {
    if(req.user.role !== 1) return res.json({status: 1, message: '非管理员，无法获取审核信息'})
    const sqlStr = 'select * from roles'
    db.query(sqlStr, (err, results) => {
        if(err) return res.json({status: 1, message: err.message})
        if(results.length === 0) return res.json({status: 1, message: '暂无任何申请'})
        res.json(results)
    })
})

module.exports = router